package com.honghe.system.service.impl;

import com.honghe.system.domain.BankCustomerCases;
import com.honghe.system.domain.BankCustomerCasesStatistic;
import com.honghe.system.mapper.StatisticMapper;
import com.honghe.system.service.IStatisticService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class StatisticServiceImpl implements IStatisticService {
    
    private static final Logger log = LoggerFactory.getLogger(StatisticServiceImpl.class);
    
    @Autowired
    private StatisticMapper statisticMapper;

    @Override
    public List<BankCustomerCases> selectStatisticCustomerCasesList(BankCustomerCases bankCustomerCases) {
        log.info("查询原表有案号的客户, 参数: {}", bankCustomerCases);
        List<BankCustomerCases> list = statisticMapper.selectStatisticCustomerCasesList(bankCustomerCases);
        log.info("查询结果数量: {}", list.size());
        return list;
    }
    
    @Override
    public List<BankCustomerCasesStatistic> selectCustomerCasesStatisticList(BankCustomerCasesStatistic statistic) {
        log.info("查询统计表, 参数: {}", statistic);
        List<BankCustomerCasesStatistic> list = statisticMapper.selectCustomerCasesStatisticList(statistic);
        log.info("查询结果数量: {}", list.size());
        return list;
    }
    
    @Override
    public BankCustomerCasesStatistic selectCustomerCasesStatisticById(Long id) {
        return statisticMapper.selectCustomerCasesStatisticById(id);
    }
    
    @Override
    @Transactional
    public int syncCustomerCasesStatistic(String username) {
        log.info("开始同步数据到统计表, 操作人: {}", username);
        try {
            // 先清空统计表
            statisticMapper.truncateCustomerCasesStatistic();
            log.info("已清空统计表");
            
            // 同步数据
            int rows = statisticMapper.syncCustomerCasesStatistic();
            log.info("成功同步 {} 条记录到统计表", rows);
            
            return rows;
        } catch (Exception e) {
            log.error("同步数据失败", e);
            throw e;
        }
    }
    
    @Override
    public int deleteCustomerCasesStatisticByIds(Long[] ids) {
        log.info("删除统计表数据, IDs: {}", (Object) ids);
        return statisticMapper.deleteCustomerCasesStatisticByIds(ids);
    }
    
    @Override
    public int insertCustomerCasesStatistic(BankCustomerCasesStatistic statistic) {
        statistic.setSyncTime(new Date());
        return statisticMapper.insertCustomerCasesStatistic(statistic);
    }
    
    @Override
    public int updateCustomerCasesStatistic(BankCustomerCasesStatistic statistic) {
        return statisticMapper.updateCustomerCasesStatistic(statistic);
    }
} 